package com.wolfpire.section.service.impl;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import com.wolfpire.chapter.dao.ChapterDao;
import com.wolfpire.chapter.dao.SubjectChapterRelationDao;
import com.wolfpire.profession.dao.ProfessionDao;
import com.wolfpire.section.dao.ChapterSectionRelationDao;
import com.wolfpire.section.dao.SectionDao;
import com.wolfpire.section.dao.StudyHistoryDao;
import com.wolfpire.section.service.SectionService;
import com.wolfpire.subject.dao.ProfessionSubjectRelationDao;
import com.wolfpire.subject.dao.SubjectDao;
import com.wolfpire.subject.service.SubjectService;
import com.wolfpire.system.common.Constants;
import com.wolfpire.system.common.MD5;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.base.dao.IBaseHibernateDao;
import com.wolfpire.system.common.base.service.impl.BaseHibernateService;
import com.wolfpire.system.common.util.StringUtils;
import com.wolfpire.system.dao.AuthorityDao;
import com.wolfpire.system.dao.OrgDao;
import com.wolfpire.system.dao.OrgUserRelationDao;
import com.wolfpire.system.dao.RoleDao;
import com.wolfpire.system.dao.UserDao;
import com.wolfpire.system.dao.UserRoleRelationDao;
import com.wolfpire.system.model.Attachment;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.Chapter;
import com.wolfpire.system.model.ChapterSectionRelation;
import com.wolfpire.system.model.Org;
import com.wolfpire.system.model.OrgUserRelation;
import com.wolfpire.system.model.Profession;
import com.wolfpire.system.model.ProfessionSubjectRelation;
import com.wolfpire.system.model.Section;
import com.wolfpire.system.model.Subject;
import com.wolfpire.system.model.SubjectChapterRelation;
import com.wolfpire.system.model.User;
import com.wolfpire.system.model.UserRoleRelation;
import com.wolfpire.system.service.AttachmentService;
import com.wolfpire.system.service.UserService;

@Service("sectionService")
public class SectionServiceImpl extends BaseHibernateService<Section, Long> implements
	SectionService {
	
	@Resource private SectionDao sectionDao;
	@Resource private ChapterDao chapterDao;
	@Resource private SubjectDao subjectDao;
	@Resource private ProfessionDao professionDao;
	@Resource private ChapterSectionRelationDao chapterSectionRelationDao;
	@Resource private AttachmentService attachmentService;
	@Resource private StudyHistoryDao studyHistoryDao;
	
	public List<Section> list(Section entity) {
		// TODO Auto-generated method stub
		return null;
	}
	public Section getByName(String name) {
		// TODO Auto-generated method stub
		return null;
	}
	public Page<Section> setPageDataList(Page<Section> page,
			Section filterSection) {
		// TODO Auto-generated method stub
		return this.sectionDao.findSections(page, filterSection);
	}
	public List<Section> getSectionsBychapterId(Long chapterId) {
		// TODO Auto-generated method stub
		return null;
	}
	public Section getCompleteSection(Long id) {
		Section section = this.get(id);
		// 绑定专业
		ChapterSectionRelation cs = this.chapterSectionRelationDao.getCSRelation(id);
		section.setChapterId(cs.getChapterId());
		//Subject subject = this.subjectDao.get(sc.getSubjectId());
		/*if (null != subject) {
			knowledge.setName(subject.getName());
		}*/
		return section;
	}
	
	public void saveOrUpdate(Section section) {
		if (null == section.getId()) {
			this.sectionDao.saveOrUpdate(section);
		} else {
			this.sectionDao.merge(section);
		} 
		
		//this.attachmentService.batchRelateAttachment(section.getAttId(), section.getId(), "section");
		this.attachmentService.batchRelateAttachment(section.getAttaFiles(), section.getId(), "section");
		this.attachmentService.batchRelateAttachment(section.getAttaVideoImg(), section.getId(), "videoImage");
		
		Long sectionId = section.getId();
		Long chapterId = section.getChapterId();
		this.chapterSectionRelationDao.relate(chapterId, sectionId);
	}
	
	public List<Map<String, Object>> bulidTree(Section section) {
		Profession profession = new Profession();
		List<Profession> pList = this.professionDao.list(profession );
		List<Map<String, Object>> subjects = this.subjectDao.querySubjectFullInfo();
		List<Map<String, Object>> chapters = this.chapterDao.queryChapterFullInfo();         //章
		List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
		for (Profession p : pList) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", p.getId());
			map.put("text", p.getName());
			map.put("nodeType", "profession");
			map.put("state", "open");
			List<Map<String, Object>> children = new ArrayList<Map<String,Object>>();
			for (Map<String,Object> sub : subjects) {
				if (p.getId().toString().equals(sub.get("profession_id").toString())) {
					Map<String, Object> subMap = new HashMap<String, Object>();
					subMap.put("id", sub.get("id"));
					subMap.put("text", sub.get("name"));
					subMap.put("state", "open");
					subMap.put("nodeType", "subject");
					children.add(subMap);
				}
				for(Map<String,Object> chapter : chapters){
					if(sub.get("id").equals(chapter.get("subject_id").toString())){
						Map<String, Object> chapterMap = new HashMap<String, Object>();
						chapterMap.put("id", chapter.get("id"));
						chapterMap.put("text", chapter.get("name"));
						chapterMap.put("state", "open");
						chapterMap.put("nodeType", "chapter");
						children.add(chapterMap);
					}
				}
				
			}
			
			map.put("children", children);
			lists.add(map);
		}
		return lists;
	}
	public Section getById(long id) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	protected IBaseHibernateDao<Section, Long> getEntityDao() {
		// TODO Auto-generated method stub
		return this.sectionDao;
	}
	/**
	 * 根据章ID获取底下的所有节
	 */
	public List<Section> getByChapterId(long chapterId) {
		// TODO Auto-generated method stub
		return this.sectionDao.getByChapterId(chapterId);
	}
	
	/**
	 * 根據學科ID獲取章節
	 */
	public List<Map<String, Object>> getBySubjcetId(Long userId, long subjectId) {
		List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
		Subject subject = this.subjectDao.getById(subjectId);
		List<Chapter> chapters = this.chapterDao.getBySubjectId(subjectId);
		int i = 0;      //课程里面的节数	
		for(Chapter chapter : chapters){
			List<Section> sections = this.sectionDao.getByChapterId(chapter.getId());
			i = i+ sections.size();
		}
		String result = String.valueOf(0);
		if(userId != null && i != 0){                      //如果userId未空   未登陆状态
			int percent = this.studyHistoryDao.complempercent(subjectId, userId);
			NumberFormat numberFormat = NumberFormat.getInstance();  
			// 设置精确到小数点后2位  
			numberFormat.setMaximumFractionDigits(0);  
			result = numberFormat.format((float) percent / (float) i * 100);  
		}
		Map<String, Object> SubjectMap = new HashMap<String, Object>();
		SubjectMap.put("subjectId", subject.getId());                       //学科ID
		SubjectMap.put("subjectName", subject.getName());                   //学科名称
		SubjectMap.put("percent", result);                                  //学习百分比
		SubjectMap.put("filePath", subject.getImage());                       //学科封面
		if(subject.getRemark() != null && !"".equals(subject.getRemark().toString())){
			SubjectMap.put("subjectRemark", subject.getRemark());           //学科描述
		}else{
			SubjectMap.put("subjectRemark", "暂无相关的描述");
		}
		List<Attachment> attachments = this.attachmentService.getByBiz(subjectId, "subject");
		if(attachments != null && attachments.size() > 0){
			String filePath = attachments.get(0).getFilePath();
			SubjectMap.put("filePath", filePath);                          //学科的图标
		}
		lists.add(SubjectMap);
		for(Chapter chapter : chapters){
			Map<String, Object> chapterMap = new HashMap<String, Object>();
			//chapterMap.put("chapter", chapter);
			chapterMap.put("id", chapter.getId());
			chapterMap.put("name", chapter.getName());
			//lists.add(chapterMap);
			List<Section> sections = this.sectionDao.getByChapterId(chapter.getId());  //根據章ID獲取對應的節
			List<Map<String, Object>> Sectionlists = new ArrayList<Map<String,Object>>();
			for(Section section : sections){
				Map<String, Object> SectionMap = new HashMap<String, Object>();
				//SectionMap.put("section", SectionMap);
				SectionMap.put("id", section.getId());
				SectionMap.put("name", section.getName());
				SectionMap.put("title", section.getTitle());
				Sectionlists.add(SectionMap);
			}
			chapterMap.put("chapter", Sectionlists);
			lists.add(chapterMap);
		}	
		return lists;
	}
	
	public List<Map<String, Object>> getBySectionId(long sectionId, String fileType) {
		List<Map<String, Object>> lists = new ArrayList<Map<String,Object>>();
		//List<Attachment> a = this.attachmentService.getByBiz(Long.valueOf(sectionId), "section");
		List<Attachment> a = this.attachmentService.getByBizAndFileType(Long.valueOf(sectionId), "section",fileType);
		ChapterSectionRelation  chapterSectionRelation = chapterSectionRelationDao.getCSRelation(sectionId);
		Chapter chapter = this.chapterDao.get(chapterSectionRelation.getChapterId());
		Section section = this.sectionDao.get(sectionId);
		for(Attachment attachment : a){
			Map<String, Object> SectionMap = new HashMap<String, Object>();
			SectionMap.put("filePath", attachment.getFilePath());
			SectionMap.put("imgPath", attachment.getImgPath());
			SectionMap.put("fileName", attachment.getFileName());
			SectionMap.put("chapterName", chapter.getName());
			SectionMap.put("sectionName", section.getName());
			lists.add(SectionMap);
		}
		return lists;
	}
	
}
